from flask import Flask
from flask_script import Manager
from flask_sqlalchemy import SQLAlchemy
import os
import platform


class Application(Flask):
    def __init__(self, import_name, template_folder=None, root_path=None):
        # 这里static_folder=None如果不写会在项目根目录寻找static静态资源目录，找不到则报错
        super(Application, self).__init__(import_name, template_folder=template_folder, root_path=root_path, static_folder=None)
        # 加载配置文件
        self.config.from_pyfile('config/base_setting.py')  # 不管本地还是线上都需要的配置

        # # mac或者linux环境才能获取，windows获取不了
        # if "ops_config" in os.environ:  # ops_config选择 local或者production
        #     self.config.from_pyfile('config/%s_setting.py' % os.environ['ops_config'] )
        # else:  # 默认运行用线下模型
        #     self.config.from_pyfile('config/local_setting.py')

        if platform.system().lower() == "windows":
            self.config.from_pyfile('config/local_setting.py')
        elif platform.system().lower() == "linux":
            self.config.from_pyfile('config/production_setting.py')
        else:
            self.config.from_pyfile('config/local_setting.py')

        db.init_app(self)


db = SQLAlchemy()

import os
template_folder = os.getcwd() + "/web/templates/"
print('template_folder:', template_folder)
app = Application(__name__, template_folder=template_folder, root_path=os.getcwd())
manager = Manager(app)

"""
函数模板
"""
from common.libs.UrlManager import UrlManager
# 添加模板全局变量，否则html里的buildStaticUrl加载不了模板，
# 显示jinja2.exceptions.UndefinedError: 'buildStaticUrl' is undefined报错
app.add_template_global(UrlManager.buildStaticUrl, "buildStaticUrl")
app.add_template_global(UrlManager.buildUrl, "buildUrl")